package com.fr.chicken.manager.house;

import com.fr.chicken.dao.ChickenInfoDao;
import com.fr.chicken.dao.HouseInfoDao;
import com.fr.chicken.dao.MaxConfigureDao;
import com.fr.chicken.manager.configure.ConfigManager;
import com.fr.chicken.model.ChickenInfo;
import com.fr.chicken.model.HouseInfo;
import com.fr.chicken.model.MaxConfigure;
import com.fr.chicken.model.vo.ChickenInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
@Repository
public class HouseManager {
    @Autowired
    private HouseInfoDao houseInfoDao;
    @Autowired
    private ChickenInfoDao chickenInfoDao;
    @Autowired
    private MaxConfigureDao maxConfigureDao;
    @Autowired
    private ConfigManager configManager;

    public void createHouse(long playerId) {
        HouseInfo houseInfo = createHouseInfo(playerId);
        if (!insertHouseInfo(houseInfo)){
            throw new RuntimeException("创建鸡场失败");
        }
        if (!insertChickenInfo(createChickenInfo(houseInfo.getId(),(byte)2))){
            throw new RuntimeException("创建默认鸡失败");
        }
    }

    public void addChicken(long playerId,byte type){
        HouseInfo houseInfo = getHouseByPlayerId(playerId);
        if(houseInfo != null){
            insertChickenInfo(createChickenInfo(houseInfo.getId(),type));
        }
    }

    public void addChickens(long playerId,byte type,int chickenNum){
        HouseInfo houseInfo = getHouseByPlayerId(playerId);
        if(houseInfo != null && chickenNum > 0){
            List<ChickenInfoVo> chickenInfos  = new ArrayList<>();
            for(int index =0;index<chickenNum;index ++){
                chickenInfos.add(createChickenInfoVO(houseInfo.getId(),type));
            }
            insertBatchChickenInfo(chickenInfos);
        }
    }
    /**
     * 鸡场生态鸡数量是否到达上限
     * */
    public boolean isAddChicken(Integer houseId,Integer num){
        HouseInfo houseInfo = houseInfoDao.selectByPrimaryKey(houseId);
        List<ChickenInfo> chickenInfos = chickenInfoDao.selectByHouseIdAndType(houseInfo.getId(),(byte)1);
        if (houseInfo.getLevel()==0){
//            MaxConfigure maxConfigure = maxConfigureDao.selectByPrimaryKey(3);
            if (chickenInfos.size()+num>=configManager.getIntConfig(3)){
                return false;
            }
        }else if (houseInfo.getLevel()==1){
//            MaxConfigure maxConfigure = maxConfigureDao.selectByPrimaryKey(4);
            if (chickenInfos.size()+num>=configManager.getIntConfig(4)){
                return false;
            }
        }
        return true;
    }
    public HouseInfo getHouseByPlayerId(long playerId){
        return houseInfoDao.selectByPlayerId(playerId);
    }

    /**
     * 创建鸡
     * */
    private ChickenInfoVo createChickenInfoVO(int houseId,byte type){
        ChickenInfoVo result = new ChickenInfoVo();
        result.setType(type);
        result.setHouseId(houseId);
        result.setBirthday((System.currentTimeMillis() / 1000));
        result.setLastLayEggsTime((System.currentTimeMillis() / 1000));
        return result;
    }

    /**
     * 创建鸡
     * */
    private ChickenInfo createChickenInfo(int houseId,byte type){
        ChickenInfo result = new ChickenInfo();
        result.setType(type);
        result.setHouseId(houseId);
        result.setBirthday((System.currentTimeMillis() / 1000));
        result.setLastLayEggsTime((System.currentTimeMillis() / 1000));
        return result;
    }

    private HouseInfo createHouseInfo(long playerId){
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setPlayerId(playerId);
        houseInfo.setLastFaecesTime((System.currentTimeMillis() / 1000));
        houseInfo.setLastFeatherTime((System.currentTimeMillis() / 1000));
        return houseInfo;
    }

    /**
     * 插入鸡数据
     * */
    private boolean insertChickenInfo(ChickenInfo chicken){
       return chickenInfoDao.insert(chicken) >= 1;
    }

    /**
     *批量 插入鸡数据
     * */
    private boolean insertBatchChickenInfo(List<ChickenInfoVo> chickens){
        return chickenInfoDao.insertBatch(chickens) >= 1;
    }

    /**
     * 插入鸡场数据
     * */
    private boolean insertHouseInfo(HouseInfo houseInfo){
        return houseInfoDao.insertSelective(houseInfo)>=1;
    }
}
